package com.example.a12502.aihome.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.example.a12502.aihome.HomeFragment;
import com.example.a12502.aihome.R;

import java.util.ArrayList;
import java.util.HashMap;

public class ListViewAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<HashMap<String,Object>> data;
    homeBean bean=null;

    public ListViewAdapter(Context context, ArrayList<HashMap<String,Object>> data){
        this.context=context;
        this.data=data;
    }

    public void refresh(ArrayList<HashMap<String,Object>> list) {
        data = list;//传入list，然后调用notifyDataSetChanged方法
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            bean=new homeBean();
            LayoutInflater layoutInflater=LayoutInflater.from(context);
            convertView=layoutInflater.inflate(R.layout.home_item,null);
            bean.test_type=convertView.findViewById(R.id.test_type);
            bean.test_value=convertView.findViewById(R.id.test_value);
            bean.test_time=convertView.findViewById(R.id.test_time);
            bean.test_result=convertView.findViewById(R.id.test_result);
            bean.picture=convertView.findViewById(R.id.picture);
            convertView.setTag(bean);
        }else {
            bean=(homeBean)convertView.getTag();
        }
        bean.test_type.setText((String) data.get(position).get("test_type"));
        bean.test_value.setText((String) data.get(position).get("test_value"));
        bean.test_time.setText((String) data.get(position).get("test_time"));
        bean.test_result.setText((String) data.get(position).get("test_result"));
        bean.picture.setImageResource((int) data.get(position).get("picture"));

        return convertView;
    }
}
